# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/2/14 15:14
desc:
'''
import time
from jsonpath import jsonpath
from proj_spec.DAS.std import get_std_data
from proj_spec.DAS.extractdata import get_extract_data
from proj_spec.DAS.strcut import get_struct_data
from util.date_util import get_datetime_from_now


def validate_upt_updated(file_id, model_id, element_id, times):
    """验证upt被更新

    :param file_id: 文件id
    :param model_id: 模型id
    :param element_ids: 元素id的列表
    :param times: 时间的字典，键为'extract'/'struct'/'std'，值为更新时间，如：’2022-02-22 12:12:12‘

    :return:
    """
    # 等待至元素更新时间
    # 验证元素的提取、结构化、标准化时间被更新

    stages = times.keys()
    if 'extract' in stages:
        new_extract_time = jsonpath(get_extract_data(file_id, model_id, element_id), "$.upt")[0]
        n = 0
        while new_extract_time == times['extract'] and n < 6:  # 提取时间尚未更新
            time.sleep(5)
            new_extract_time = jsonpath(get_extract_data(file_id, model_id, element_id), "$.upt")[0]
            n += 1

        assert new_extract_time != times['extract'], "元素%s提取时间未更新: original time: %s, new_extract_time %s" \
                         % (element_id, times['extract'], new_extract_time)
        times['extract'] = new_extract_time

    if 'struct' in stages:
        struct_data = get_struct_data(file_id, model_id, element_id)
        upt_index = struct_data['outputHeads'].index('修改时间')
        new_struct_time_list = jsonpath(struct_data,
                                        "$.outputCells[*][%s].dataValue"%upt_index)
        new_struct_time_list.sort()
        new_struct_time = new_struct_time_list[-1]  # 最新的时间

        n = 0
        while new_struct_time == times['struct'] and n < 6:  # 结构化时间尚未更新
            time.sleep(5)
            struct_data = get_struct_data(file_id, model_id, element_id)
            upt_index = struct_data['outputHeads'].index('修改时间')
            new_struct_time_list = jsonpath(struct_data,
                                            "$.outputCells[*][%s].dataValue"%upt_index)
            new_struct_time_list.sort()
            new_struct_time = new_struct_time_list[-1]  # 最新的时间
            n += 1

        assert new_struct_time != times['struct'], "元素%s结构化时间未更新: original time: %s, new_struct_time %s, 当前时间： %s" \
                         % (element_id, times['struct'], new_struct_time, get_datetime_from_now())
        times['struct'] = new_struct_time

    if 'std' in stages:
        new_std_time = jsonpath(get_std_data(file_id, model_id, element_id), "$.upt")[0]
        n = 0
        while new_std_time == times['std'] and n < 6:  # 标准化时间尚未更新
            time.sleep(5)
            new_std_time = jsonpath(get_std_data(file_id, model_id, element_id), "$.upt")[0]
            n += 1

        assert new_std_time != times['std'], "元素%s标准化时间未更新: original time: %s, new_std_time %s" \
                         % (element_id, times['std'], new_std_time)
        times['std'] = new_std_time

    return True


def validate_upt_not_updated(file_id, model_id, element_id, times):
    """验证upt不更新

    参数列表同validate_upt_updated

    :return:
    """
    stages = times.keys()
    # 验证审核通过的元素的提取时间未更新
    new_extract_time = jsonpath(get_extract_data(file_id, model_id, element_id), "$.upt")[0]
    struct_data = get_struct_data(file_id, model_id, element_id)
    upt_index = struct_data['outputHeads'].index('修改时间')
    new_struct_time_list = jsonpath(struct_data, "$.outputCells[*][%s].dataValue"%upt_index)
    new_struct_time_list.sort()
    new_struct_time = new_struct_time_list[-1]  # 最新的时间
    new_std_time = jsonpath(get_std_data(file_id, model_id, element_id), "$.upt")[0]

    if 'extract' in stages:
        assert times['extract'] == new_extract_time, "元素%s提取时间不应被更新 original time: %s, new_extract_time %s" \
                                                % (element_id, times['extract'], new_extract_time)
    if 'struct' in stages:
        assert times['struct'] == new_struct_time, "元素%s结构化时间不应被更新 original time: %s, new_struct_time %s" % \
                                           (element_id, times['struct'], new_struct_time)
    if 'std' in stages:
        assert times['std'] == new_std_time, "元素%s标准化时间不应被更新 original time: %s, new_std_time %s" % \
                                                     (element_id, times['std'], new_std_time)


    return True